//
//  NoticeTableViewCell.m
//  ChatApp
//
//  Created by DJL on 15/12/21.
//  Copyright © 2015年 DJL. All rights reserved.
//

#import "NoticeTableViewCell.h"
#import "EaseMob.h"



@interface NoticeTableViewCell ()

@property (nonatomic, strong) UIImageView *image;

@property (nonatomic, strong) UILabel *name;

@property (nonatomic, strong) UILabel *userMessage;

@property (nonatomic, strong) UIButton *agree;

@property (nonatomic, strong) UIButton *refuse;

@end

@implementation NoticeTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.image = [[UIImageView alloc] init];
        [self.contentView addSubview:self.image];
        self.image.layer.cornerRadius = 8;
        self.image.layer.masksToBounds = YES;
        self.image.backgroundColor = [UIColor lightGrayColor];
        
        self.name = [[UILabel alloc] init];
        [self.contentView addSubview:self.name];
        self.name.font = [UIFont systemFontOfSize:20];
        
        self.userMessage = [[UILabel alloc] init];
        [self.contentView addSubview:self.userMessage];
        self.userMessage.textColor = [UIColor lightGrayColor];
        
        self.agree = [UIButton buttonWithType:UIButtonTypeSystem];
        self.agree.backgroundColor = [UIColor lightGrayColor];
        [self.agree setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.agree.layer.cornerRadius = 5;
        [self.contentView addSubview:self.agree];
        
        self.refuse = [UIButton buttonWithType:UIButtonTypeSystem];
        self.refuse.backgroundColor = [UIColor lightGrayColor];
        [self.refuse setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.refuse.layer.cornerRadius = 5;
        [self.contentView addSubview:self.refuse];
        
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.image.frame = CGRectMake(10, 10, self.contentView.frame.size.height - 20, self.contentView.frame.size.height - 20);
    
    
    self.name.frame = CGRectMake(self.image.frame.origin.x + self.image.frame.size.width + 15, 10, 150, 25);
    self.name.text = self.userName;
    
    
    self.userMessage.frame = CGRectMake(self.image.frame.origin.x + self.image.frame.size.width + 15, self.name.frame.origin.y + self.name.frame.size.height + 5, 150, 25);
    self.userMessage.text = self.message;
    
    
    self.agree.frame = CGRectMake(self.contentView.frame.size.width - 10 - 50, 20, 50, self.contentView.frame.size.height - 40);
    [self.agree setTitle:@"同意" forState:UIControlStateNormal];
    [self.agree addTarget:self action:@selector(handleAgree:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.refuse.frame = CGRectMake(self.contentView.frame.size.width - 20 - 100, 20, 50, self.contentView.frame.size.height - 40);
    [self.refuse setTitle:@"拒绝" forState:UIControlStateNormal];
    [self.refuse addTarget:self action:@selector(handleRefuse:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleAgree:(UIButton *)btn {
    
    
    EMError *error = nil;
    BOOL isSuccess = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:self.name.text error:&error];
    if (isSuccess && !error) {
        NSLog(@"发送同意成功");
    }
    if ([self.delegate respondsToSelector:@selector(pass:num:)]) {
         [self.delegate pass:YES num:self.num];
    }
   
    
}

- (void)handleRefuse:(UIButton *)btn {
    
    EMError *error = nil;
    BOOL isSuccess = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:self.name.text reason:@"111111" error:&error];
    if (isSuccess && !error) {
        NSLog(@"发送拒绝成功");
    }
    if ([self.delegate respondsToSelector:@selector(pass:num:)]) {
        [self.delegate pass:NO num:self.num];
    }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
